﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using System.Web.Security;
using Telerik.Web.UI;
using BSCMFF.Web.UserControls.Details;
using BSCMFF.Core.Exceptions.Database;

namespace BSCMFF.Web.Pages.working
{
    public partial class personaltabneide : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RadGridPersonalKPIs.Culture = System.Threading.Thread.CurrentThread.CurrentCulture;

            if (!Roles.IsUserInRole("Manager"))
            {
                //HideEditAndDeleteButtons();
            }

            //if (!IsPostBack)
            //{
            //if (Request.QueryString["EmployeeID"] != null)
            //{
            //int employeeID = Convert.ToInt32(Request.QueryString["EmployeeID"]);
            //BSCEntities context = new BSCEntities();


            //List<Personal_KPI> pKPIs = (from pkpi in context.Personal_KPI.Include("KPI")
            //                            where pkpi.User_FK == employeeID
            //                            select pkpi).ToList();
            //RadGridPersonalKPIs.DataSource = pKPIs;
            //RadGridPersonalKPIs.DataBind();

            //}
            // }
        }

        private void HideEditAndDeleteButtons()
        {
            //RadGridPersonalKPIs.showe
            RadGridPersonalKPIs.AutoGenerateEditColumn = false;
            RadGridPersonalKPIs.AutoGenerateDeleteColumn = false;
        }

        protected void RadGridPersonalKPIs_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            PKPIDetails userControl = (PKPIDetails)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);



            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Personal_KPI_ID"].ToString());
            BSCEntities context = new BSCEntities();

            Personal_KPI pkpi = (from p in context.Personal_KPI
                                 where p.Personal_KPI_ID == id
                                 select p).SingleOrDefault();

            if (pkpi != null)
            {
                pkpi.Name = userControl.Name;
                pkpi.Description = userControl.Description;
                //pkpi.Weight = userControl.Weight;
                pkpi.Measure = userControl.Measure;
                pkpi.StartValue = (int)userControl.StartValue;
                pkpi.TargetValue = (int)userControl.TargetValue;
                pkpi.CurrentValue = (int)userControl.CurrentValue;

                //pkpi.User_FK = Convert.ToInt32((userControl.FindControl("ComboBoxUsers") as RadComboBox).SelectedValue);

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new InconsistentDatabaseException("Couldn't save personal KPI");
                }

            }

        }

        protected void RadGridPersonalKPIs_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            PKPIDetails userControl = (PKPIDetails)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);



            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Personal_KPI_ID"].ToString());
            BSCEntities context = new BSCEntities();

            Personal_KPI pkpi = (from p in context.Personal_KPI
                                 where p.Personal_KPI_ID == id
                                 select p).SingleOrDefault();

            if (pkpi != null)
            {

                context.DeleteObject(pkpi);

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new InconsistentDatabaseException("Couldn't delete personal KPI");
                }
            }
        }

        int totalWeight = 0;
        protected void RadGridPersonalKPIs_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item is GridDataItem)
            //{
            //    GridDataItem dataItem = e.Item as GridDataItem;
            //    int fieldValue = int.Parse(dataItem["Weight"].Text);
            //    totalWeight += fieldValue;
            //}
            //if (e.Item is GridFooterItem)
            //{
            //    GridFooterItem footerItem = e.Item as GridFooterItem;
            //    footerItem["Weight"].Text = "total: " + totalWeight.ToString();
            //}
        }

    }
}